<?php

class ProfileController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';
	public $optionsPrivacy=array('0'=>'Никто', '1'=>'Все пользователи', '2'=>'Только друзья');
	public $optionsAlertEvent=array('0'=>'Не оповещать', '1'=>'Оповещать обо всем');
	public $optionsAlertEmail=array('0'=>'Никогда не оповещать', '1'=>'Всегда оповещать', '2'=>'Оповещать не чаще раза в день');

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('uploadAvatar','uploadAvatarImg', 'insertcomment', 'deletecomment', 'insertcommentlike', 'deletecommentlike', 'morecomments', 'updatestatus', 'getimagesforcomment', 'getvideosforcomment', 'getmusicsforcomment', 'autocompletefriends', 'deletefriend', 'addfriend', 'confirmfriend', 'checkrequestfriend'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index','create','update', 
					'settings','settingsPersonal','settingsGeneral','settingsPrivacy','settingsAlert','addBlackList','blackList','deleteBlackList'
				),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	
	public function actionUploadAvatar()
	{//
		$this->renderPartial('uploadAvatar',array(
			'model'=>$this->loadModel(Yii::app()->user->id)
		));
	}
	public function actionUploadAvatarImg(){
		$model=new Photos;
		$model->attributes = array('image'=>'');
		$model->album_id = '0';
		$model->user_id = Yii::app()->user->id;
		$model->crdate = date("Y-m-d H:i:s");
		$path = $_SERVER['DOCUMENT_ROOT'].'/upload/photos/'.Yii::app()->user->id.'/0/';
		$model->name = self::savePhoto($_FILES['image'], $path);
		$model->save();		

		$avatar_id = $model->getPrimaryKey();
		
		$make = self::loadModel(Yii::app()->user->id);
		$make->avatar = $avatar_id;
		$make->save();
		
		Yii::app()->user->setState('avatar', $model->name);
		echo "{";
		echo	"msg: '" . $avatar_id . "'\n";
		echo "}";
	}
	public function actionIndex()
	{	
		$id = Yii::app()->request->getParam('id');
		if(!$id){
			$id = Yii::app()->user->id;
			$_GET['id'] = $id;
		}
		$user = self::loadModel($id);
		
		if($id==Yii::app()->user->id) {
			$user->last_visit = date("Y-m-d H:i:s");
			$user->save();
			$comments = $user->comments;
			$countComments = $user->countComments;
		} else {
			$model = $this->loadModel(Yii::app()->user->id);
			$model->last_visit = date("Y-m-d H:i:s");
			$model->save();
			
			if($user->wall_my_records == 1){
				$comments = UsersComments::model()->findAll(array(
					'limit' 	=>	10,
					'order' 	=>	'id DESC',
					'condition'	=>	'(user_id=:user_id OR user_id=:profile_id) AND parent_id=0 AND profile_id=:profile_id',
					'params'	=>	array(':profile_id'=>$id, ':user_id'=>Yii::app()->user->id)
				));
				$countComments = count(UsersComments::model()->findAll(array(
					'condition'	=>	'(user_id=:user_id OR user_id=:profile_id) AND profile_id=:profile_id',
					'params'	=>	array(':profile_id'=>$id, ':user_id'=>Yii::app()->user->id)
				)));
			}else{
				$comments = $user->comments;
				$countComments = $user->countComments;
			}
		}
		$friends_online = Friends::model()->with('friend_info')->findAll(
			'friend_info.last_visit > :last_visit AND user_id=:user_id',
			array(
					':last_visit' => date("Y-m-d H:i:s", time() - 15*60),
					':user_id' => $id
				)
		);
		
		$percent = 45;
		$percent = $user->countAlbums > 0 ? $percent + 15 : $percent; 
		$percent = $user->birthday !=NULL ? $percent + 10 : $percent; 
		$percent = $user->teach !=NULL ? $percent + 10 : $percent; 
		$percent = ($user->country_id !=NULL && $user->city !=NULL && $user->address !=NULL && $user->phone !=NULL) ? $percent + 10 : $percent;
		$percent = $user->countGroups > 0 ? $percent + 10 : $percent; 
		$this->render('index',array(
			'data'=>$user,
			'comments'=>$comments,
			'countComments'=>$countComments,
			'percent'=>$percent,
			'friends_online'=>$friends_online,
		));	
	}

	
	// more comments
	public function actionMoreComments(){
		if (Yii::app()->request->isAjaxRequest) {
			if(isset($_GET['id'])){

				$parent_id = (isset($_GET['parent_id'])) ? $_GET['parent_id'] : 0;
				$offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0;
				$limit = (isset($_GET['limit'])) ? $_GET['limit'] : 0;
			
				$comments=UsersComments::model()->findAll(array(
					'order'=> 'id DESC',
					'limit'=> $limit,
					'offset'=> $offset,
					'condition'=>'profile_id=:profile_id AND parent_id=:parent_id',
					'params'=>array(':profile_id'=>$_GET['id'], ':parent_id'=>$parent_id)
				));
				
				if($_GET['id']!=Yii::app()->user->id) {
					$user = $this->loadModel($_GET['id']);
						
					if($user->wall_my_records == 1){
						$comments=UsersComments::model()->findAll(array(
							'order'=> 'id DESC',
							'limit'=> $limit,
							'offset'=> $offset,
							'condition'=>'(user_id=:user_id OR user_id=:profile_id) AND parent_id=:parent_id AND profile_id=:profile_id',
							'params'=>array(':profile_id'=>$_GET['id'], ':user_id'=>Yii::app()->user->id, ':parent_id'=>$parent_id)
						));						
					}
				}	

				$this->renderPartial('_commentList',array(
					'comments'=>$comments,
					'wall_comments' => (isset($_GET['wall_comments'])) ? $_GET['wall_comments'] : 0,
					'isFriend' => (isset($_GET['isFriend'])) ? $_GET['isFriend'] : 0,
					'countComments' => (isset($_GET['countComments'])) ? $_GET['countComments'] : 0,
					'parent_id' => $parent_id,
					'offset' => $offset + $limit,
					'limit' => $limit
				));
			}
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}
	
	
	// add comment
    public function actionInsertComment()
    {
    	if (Yii::app()->request->isAjaxRequest) {
	    	$data = array();
	    	
	        if(isset($_POST['text'])){
	        	$data['text'] = $_POST['text'];
	        }else{
	        	$data['text'] = '';
	        } 
	        
	        if(isset($_GET['id'])){
	        	$data['id'] = $_GET['id'];
	        }else{
	        	$data['id'] = 0;
	        }
	        
	        if(isset($_POST['from'])){
	        	$data['from_id'] = $_POST['from'];
	        }else{
	        	$data['from_id'] = NULL;
	        }    

	        if(isset($_POST['fromgroup'])){
	        	$data['group_id'] = $_POST['fromgroup'];
	        }else{
	        	$data['group_id'] = NULL;
	        }

	        if(isset($_POST['fromevent'])){
	        	$data['event_id'] = $_POST['fromevent'];
	        }else{
	        	$data['event_id'] = NULL;
	        }	        
	        
	        if(isset($_GET['parent_id'])){
	        	$data['parent_id'] = $_GET['parent_id'];
	        }else{
	        	$data['parent_id'] = 0;
	        }   

	        if(isset($_POST['images'])){
	        	$images = explode(",", $_POST['images']);
	        }else{
	        	$images = array();
	        }	

	        if(isset($_POST['videos'])){
	        	$videos = explode(",", $_POST['videos']);
	        }else{
	        	$videos = array();
	        }	        
	        
	        if(isset($_POST['musics'])){
	        	$musics = explode(",", $_POST['musics']);
	        }else{
	        	$musics = array();
	        }
	        	        
	        if($data['id']){
	        	$make = new UsersComments;
	        	$make->text = $data['text'];
	        	$make->profile_id = $data['id'];
	        	$make->user_id = Yii::app()->user->id;
	        	$make->parent_id = $data['parent_id'];
	        	$make->from_id = $data['from_id'];
	        	$make->group_id = $data['group_id'];
	        	$make->event_id = $data['event_id'];
	        	$make->crdate = date("Y-m-d H:i:s");
	        		    		        	
	        	$make->save();

	        	$data['comment_id'] =  $make->getPrimaryKey();
	        	
	        	$data['images'] = array();
	        	
        		foreach($images as $image_id){
        			if($image_id){
        				$make = new UsersCommentsPhotos;
        				$make->user_comment_id = $data['comment_id'];
        				$make->photo_id = $image_id;
        					
        				$make->save();		
        					
        				$data['images'][] = Photos::model()->findByPk($image_id);
        			}
        		}
	        	
	        	$data['videos'] = array();
	        	
        		foreach($videos as $video_id){
        			if($video_id){
        				$make = new UsersCommentsVideos;
        				$make->user_comment_id = $data['comment_id'];
        				$make->video_id = $video_id;
        	
        				$make->save();
        	
        				$data['videos'][] = Video::model()->findByPk($video_id);
        			}
        		} 

	        	$data['musics'] = array();
	        	
        		foreach($musics as $music_id){
        			if($music_id){
        				$make = new UsersCommentsMusics;
        				$make->user_comment_id = $data['comment_id'];
        				$make->music_user_id = $music_id;
        				
        				$make->save();

						$data['musics'][] = MusicUser::model()->findByPk($music_id);
        			}
        		}      	
	        	
	        	$user_info = $this->loadModel(Yii::app()->user->id);
	        	
	        	$data['user'] = array(
	        		'name' => $user_info->full_name,
	        		'id' => $user_info->id,
	        		'avatar' => $user_info->avatar
	        	);

	        	$email_to = Users::model()->findByPk( $data['id'], array('select' => 'full_name, email, wall_notification, last_visit') );
	      
				if($email_to->wall_notification == 1 && ($email_to->last_visit < date("Y-m-d H:i:s", time() - 15*60)) && !$data['parent_id']) {
					Yii::app()->getModule('email');
					$email = new Email;
					$email->from = $user_info->email;
					$email->subject = 'Вчечне';
					$email->layout = 'common';
					$email->type = 'text/html';
					$email->to = $email_to->email;
					$email->view = 'main';
					$email->send(array('header' => 'Новая запись на Вашей стене',
						'content' => $email_to->full_name . ', вы получили новое сообещение от '
							. $user_info->full_name . '<br>'
							. $data['text'] . '<br>'
							. 'Получено ' . Yii::app()->dateFormatter->formatDateTime(date("Y-m-d H:i:s"), 'long','short')
					));
				}
		          
	 			$this->renderPartial('_newComment',array(
					'comment'=>$data
				));
	        }
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }	
	
    
    // delete comment
    public function actionDeleteComment(){
    	if (Yii::app()->request->isAjaxRequest) {
    		if(isset($_GET['comment_id'])){
    			UsersComments::model()->deleteAll("id = '" . $_GET['comment_id'] . "'");
    			UsersComments::model()->deleteAll("parent_id = '" . $_GET['comment_id'] . "'");
    			UsersCommentsLikes::model()->deleteAll("user_comment_id = '" . $_GET['comment_id'] . "'");    
    			UsersCommentsPhotos::model()->deleteAll("user_comment_id = '" . $_GET['comment_id'] . "'");    
    			UsersCommentsMusics::model()->deleteAll("user_comment_id = '" . $_GET['comment_id'] . "'");
    			UsersCommentsVideos::model()->deleteAll("user_comment_id = '" . $_GET['comment_id'] . "'");
    		} 
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}    	
    }
    
    
    // comment like add
    public function actionInsertCommentLike(){
    	if (Yii::app()->request->isAjaxRequest) {
    		if(isset($_GET['comment_id'])){	
    			$make = new UsersCommentsLikes;
    			$make->user_comment_id = $_GET['comment_id'];
    			$make->user_id = Yii::app()->user->id;
    			
    			$make->save();  

    			$comment_like_id = $make->getPrimaryKey();
    			
    			$data['href_dislike'] = Yii::app()->createUrl('profile/deletecommentlike', array('comment_id'=>$_GET['comment_id'], 'comment_like_id'=>$comment_like_id));
    			
    			echo CJSON::encode($data);
    		}
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}    	
    }
    
    // comment like delete
    public function actionDeleteCommentLike(){
    	if (Yii::app()->request->isAjaxRequest) {
    		if(isset($_GET['comment_like_id'])){
    			UsersCommentsLikes::model()->deleteAll("id = '" . $_GET['comment_like_id'] . "'");
    
    			$data['href_like'] = Yii::app()->createUrl('profile/insertcommentlike', array('comment_id'=>$_GET['comment_id']));
    			 
    			echo CJSON::encode($data);
    		}
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }   
    
    // update Status
    public function actionUpdateStatus(){
    	if (Yii::app()->request->isAjaxRequest) {
    		if(isset($_POST['status'])){
    			$make = $this->loadModel($_GET['id']);
    			$make->status = $_POST['status'];
    			if($make->validate()){
    				$make->save();
    			}
    		}
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }   
    
    // get Users Images For Comment
    public function actionGetImagesForComment(){
    	if (Yii::app()->request->isAjaxRequest) {
    		$albums=Albums::model()->findAll(array(
    			'order'=> 'id DESC',
    			'condition'=>'user_id=:user_id',
    			'params'=>array(':user_id'=>Yii::app()->user->id)
    		));		
    		
    		$images = array();
    		
    		foreach($albums as $album){
    			foreach($album->photos as $photo){
    				$images[] = $photo;
    			}
    		}
    		
			$this->renderPartial('_imageForComment',array(
				'images'=>$images
			));
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}   	
    }
    
    // get Users Videos For Comment
    public function actionGetVideosForComment(){
    	if (Yii::app()->request->isAjaxRequest) {
    		$videos=VideoUser::model()->findAll(array(
    			'order'=> 'id DESC',
    			'condition'=>'user_id=:user_id AND status=1',
    			'params'=>array(':user_id'=>Yii::app()->user->id)
    		));
    
    		$this->renderPartial('_videoForComment',array(
    			'videos'=>$videos
    		));
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }  

    // get Users Musics For Comment
    public function actionGetMusicsForComment(){
    	if (Yii::app()->request->isAjaxRequest) {
    		
    		$musics=MusicUser::model()->findAll(array(
    			'order'=> 'id DESC',
    			'condition'=>'user_id=:user_id AND status=1',
    			'params'=>array(':user_id'=>Yii::app()->user->id)
    		));
    
    		$this->renderPartial('_musicForComment',array(
    			'musics'=>$musics
    		));
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }    
    
    // Autocomplete Friends
    public function actionAutocompleteFriends(){
    	if (Yii::app()->request->isAjaxRequest) {
			$friends=Friends::searchFriends(array(
				'user_id' => Yii::app()->user->id,
				'status'=>'1',
				'filter_name' => (isset($_GET['filter_name'])) ? $_GET['filter_name'] : '',
				'filter_message' => true
			));
    	
    		echo CJSON::encode($friends);
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}    	
    }
    
    
    public function actionDeleteFriend(){
    	if (Yii::app()->request->isAjaxRequest) {
    		Friends::model()->deleteAll("(`user_id` = '" . $_GET['profile'] . "' AND `friend_id` = '" . $_GET['friend_id'] . "') OR (`user_id` = '" . $_GET['friend_id'] . "' AND `friend_id` = '" . $_GET['profile'] . "')");
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }
    
    public function actionAddFriend(){
    	if(isset($_GET['friend_id'])){
    		$isRequest=Friends::model()->findAll(array(
    			'condition'=>'user_id=:user_id AND friend_id=:friend_id',
    			'params'=>array(':user_id'=>$_GET['friend_id'], ':friend_id'=>Yii::app()->user->id)
    		));
    		
    		Friends::model()->deleteAll("(`user_id` = '" . Yii::app()->user->id . "' AND `friend_id` = '" . $_GET['friend_id'] . "') OR (`user_id` = '" . $_GET['friend_id'] . "' AND `friend_id` = '" . Yii::app()->user->id . "')");
	    	
    		$make = new Friends;
    		$make->user_id = Yii::app()->user->id;
    		$make->friend_id = $_GET['friend_id'];
    		$make->status = ($isRequest) ? 1 : 0;
    		$make->save();
    		
    		if($isRequest){
	    		$make = new Friends;
	    		$make->user_id = $_GET['friend_id'];
	    		$make->friend_id = Yii::app()->user->id;
	    		$make->status = 1;
	    		$make->save();    		
    		}
    		
	    	$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>$_GET['friend_id'])));
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }    
    
    public function actionConfirmFriend(){
    	if (Yii::app()->request->isAjaxRequest) {
    		Friends::model()->deleteAll("(`user_id` = '" . $_GET['profile'] . "' AND `friend_id` = '" . $_GET['friend_id'] . "') OR (`user_id` = '" . $_GET['friend_id'] . "' AND `friend_id` = '" . $_GET['profile'] . "')");
    			
    		$make = new Friends;
    		$make->user_id = $_GET['profile'];
    		$make->friend_id = $_GET['friend_id'];
    		$make->status = 1;
    		$make->save();
    
    		$make = new Friends;
    		$make->user_id = $_GET['friend_id'];
    		$make->friend_id = $_GET['profile'];
    		$make->status = 1;
    		$make->save();
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }
    
    public function actionCheckRequestFriend(){
    	if (Yii::app()->request->isAjaxRequest) {
	    	$countData = Friends::model()->countFriends(array(
	    		'status' => 0,
	    		'friend_id' => Yii::app()->user->id
	    	));
  		
    		echo CJSON::encode($countData['total']);
    	}else{
    		$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
    	}
    }    
    
	public function actionSettings($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Users'])){
			$model->attributes=$_POST['Users'];
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/photos/';
			$fileName = self::saveImage($model->avatar, $model, $path);

			if($model->oldPassword != NULL) {
				$model->oldPassword = Users::model ()->ganaratePassword($model->oldPassword);
				if( $model->validate('oldPassword') ) {
					if( $model->validate('confirmPassword')  ) {
						$model->password = Users::model ()->ganaratePassword($model->newPassword);
					}
				} else {$model->oldPassword =NULL;}
			}

			//if($model->oldEmail != NULL) {
				//if($model->validate('oldEmail')) {
					if($model->newEmail != NULL && $model->validate('newEmail'))
						$model->email = $model->newEmail;
				//}
			//}

			
			if($model->save()){
				$this->redirect(array('index','id'=>$model->id));
			}
		}
		$this->render('settings',array(
			'model'=>$model,
		));
	}
	public function actionSettingsPersonal()
	{
		$id = Yii::app()->user->id;
		$model=$this->loadModel($id);
		if(isset($_POST['Users']))
		{
			if(empty($_POST['Users']['birthday'])){
				$_POST['Users']['birthday'] = '0000-00-00';
			}

			$model->attributes=$_POST['Users'];
			$model->full_name = $model->first_name . ' ' . $model->last_name;
			if($model->save()){
				$this->redirect(array('index','id'=>$model->id));
			}
		}
		$this->render('_settingsPersonal',array(
			'model'=>$model,
		));
	}
	public function actionSettingsGeneral()
	{
		$id = Yii::app()->user->id;
		$model=$this->loadModel($id);

		if(isset($_POST['Users'])){
			$_POST['Users']['wall_my_records'] = (isset($_POST['Users']['wall_my_records'])) ? 1 : 0;
			$_POST['Users']['wall_comments']   = (isset($_POST['Users']['wall_comments']))   ? 1 : 0;
			
			$model->attributes=$_POST['Users'];
			$model->oldPassword=$_POST['Users']['oldPassword'];		

			// Змінити пароль
			if($model->oldPassword != NULL) {
				$model->oldPassword = Users::model ()->ganaratePassword($model->oldPassword);
				if( $model->validate('oldPassword') ) {
					if( $model->validate('confirmPassword')  ) {
						$model->password = Users::model ()->ganaratePassword($model->newPassword);
					}
				}
			}
			// Змінити Email
			if($model->oldEmail != NULL) {
				if($model->validate('oldEmail')) {
					if($model->newEmail != NULL && $model->validate('newEmail'))
						$model->email = $model->newEmail;
				}
			}
			if($model->save()){
				$this->redirect(array('index','id'=>$model->id));
			} else {
				$model->oldPassword =NULL;
			}
			$this->redirect(array('index','id'=>$model->id));
		}
		$this->render('_settingsGeneral',array(
			'model'=>$model,
		));
	}

	public function actionSettingsPrivacy()
	{
		$id = Yii::app()->user->id;
		$model=$this->loadModel($id);
		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			if($model->save()){
				$this->redirect(array('index','id'=>$model->id));
			}
		}
		$this->render('_settingsPrivacy',array(
			'model'=>$model,
		));
	}

	public function actionSettingsAlert()
	{
		$id = Yii::app()->user->id;
		$model=$this->loadModel($id);
		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			if($model->save()){
				$this->redirect(array('index','id'=>$model->id));
			}
		}
		$this->render('_settingsAlert',array(
			'model'=>$model,
		));
	}

	public function actionBlackList()
	{
		$search = new SearchUserBan;

	    if(isset($_POST['SearchUserBan']))
	    {
	        $search->attributes=$_POST['SearchUserBan'];
	        if($search->validate())
	            $this->redirect(array('search/index','text'=>$search->username,'from'=>'blacklist'));
	    }
	    else 
	    	$search->username = 'Начните вводить имя друга';

	    $model = UsersBlackList::model()->findAll(
	    	'user_id=:user_id',
	    	array(':user_id'=>Yii::app()->user->id)
	    );

		$this->render('_settingsBlackList',array(
			'search'=>	$search,
			'model'	=>	$model,
		));
		
	}
	public function actionAddBlackList()
	{
		$model = new UsersBlackList;
		$model->user_id = Yii::app()->user->id;
		$model->ban_id = Yii::app()->request->getParam('blacklist');
		$model->save();

		$this->redirect(Yii::app()->createUrl('profile/BlackList', array('id'=>Yii::app()->user->id)));
	}
	public function actionDeleteBlackList($blacklist, $profile)
	{
		$model = UsersBlackList::model()->find(
			'user_id=:user_id AND ban_id=:ban_id',
			array(
				':user_id'	=>	$profile,
				':ban_id'	=>	$blacklist,
			)
		);
		if($model) $model->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			// $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('update', 'id'=>$id));
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : Yii::app()->request->urlReferrer);
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Users::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
